﻿using System.ServiceModel;
using MediaBrowserSL.BOL;
using MediaBrowserSL.DAL.Contexts;
using System;

namespace MediaBrowserSL.Services
{
    public partial class MediaBrowserService
    {
        /// <summary>
        /// Exist User
        /// </summary>
        /// <param name="Login"></param>
        /// <returns></returns>
        public User ExistUser(string Login, string password)
        {
            User user = null;
            try
            {
                user = locatorLayers.AuthenticationManager.ExistUser(Login);
                if (user != null)
                {
                    if (!user.Password.Equals(password))
                    {
                        user = null;
                    }
                }
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }

            return user;
        }

        /// <summary>
        /// Create new User
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="Login"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int CreateUser(string firstName, string lastName, string Login, string password)
        {
            int idUser;
            try
            {
                idUser = locatorLayers.AuthenticationManager.CreateUser(firstName, lastName, Login, password);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }

            return idUser;
        }

        /// <summary>
        /// Update Statut User
        /// </summary>
        /// <param name="u"></param>
        public User UpdateStatut(User u)
        {
            try
            {
                locatorLayers.AuthenticationManager.UpdateStatut(u);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }

            return u;
        }

        /// <summary>
        /// Exist Login
        /// </summary>
        /// <param name="Login"></param>
        /// <returns></returns>
        public bool ExistLogin(string Login)
        {
            bool exist;
            try
            {
                exist = locatorLayers.AuthenticationManager.ExistLogin(Login);
            }
            catch (PersistenceException p)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = p.Message
                }, new FaultReason("PersistenceException"));

            }
            catch (SqlException s)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = s.Message
                }, new FaultReason("SqlException"));
            }
            catch (Exception ex)
            {
                throw new FaultException<ErrorDataContract>(new ErrorDataContract()
                {
                    Detail = ex.Message
                }, new FaultReason("Exception"));
            }

            return exist;
        }
    }
}